﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Medical.Business;

/// <summary>
/// 分类信息列表页
/// </summary>
public partial class SortInfoList : AdminPage
{
    /// <summary>
    /// 分类类型
    /// </summary>
    protected ArticelTypeEnum eType;

    protected void Page_Load(object sender, EventArgs e)
    {
        eType = (ArticelTypeEnum)Convert.ToInt32(Common.GetQueryValue("type", 0));
        if (!Page.IsPostBack)
        {
            //页面数据初始化
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.Sort;
    }

    /// <summary>
    /// 页面数据初始化
    /// </summary>
    protected void InitData()
    {
        //分类列表绑定
        DataTable dt = new BisSortInfo().GetAllSortList(eType);
        if (dt.Rows.Count == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptSort.DataSource = dt;
        rptSort.DataBind();
    }

    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptSort_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        ImageButton btnUp = e.Item.FindControl("btnUp") as ImageButton;
        ImageButton btnDown = e.Item.FindControl("btnDown") as ImageButton;
        int nItemID = e.Item.ItemIndex;
        DataRow[] drList = dtSort.Select("ParentID=" + dtSort.Rows[nItemID]["ParentID"]);
        int nCount = drList.Length;
        int nItem = 0;
        foreach (DataRow dr in drList)
        {
            if (dr["ID"].ToString() == dtSort.Rows[nItemID]["ID"].ToString())
            {
                break;
            }
            nItem++;
        }
        //上移
        if (nCount > (nItem + 1))
        {
            btnDown.CommandArgument = dtSort.Rows[nItemID]["ID"].ToString() + ","
                                    + drList[nItem + 1]["OrderNum"].ToString() + ","
                                    + drList[nItem + 1]["ID"].ToString() + ","
                                    + dtSort.Rows[nItemID]["OrderNum"].ToString();
        }
        //下移
        if ((nItem - 1) >= 0)
        {
            btnUp.CommandArgument = dtSort.Rows[nItemID]["ID"].ToString() + ","
                                  + drList[nItem - 1]["OrderNum"].ToString() + ","
                                  + drList[nItem - 1]["ID"].ToString() + ","
                                  + dtSort.Rows[nItemID]["OrderNum"].ToString();
        }
        if (nItem == 0)
        {
            //禁用第一个上移按钮
            btnUp.Enabled = false;
            btnUp.ImageUrl = "images/noup.gif";
            btnUp.CommandArgument = "";

        }
        if (nItem == (nCount - 1))
        {
            //禁用最后一个下移按键
            btnDown.Enabled = false;
            btnDown.ImageUrl = "images/nodown.gif";
            btnDown.CommandArgument = "";
        }
        if (Convert.ToInt32(dtSort.Rows[nItemID]["ParentID"]) == 0)
        {
            if (eType == ArticelTypeEnum.Health)
            {
                ImageButton btnDelete = e.Item.FindControl("btnDelete") as ImageButton;
                btnDelete.Visible = false;
            }
        }
    }

    /// <summary>
    /// 分类列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptSort_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        BisSortInfo bisSortInfo = new BisSortInfo();
        bool result = false;
        if (e.CommandName == "OrderUp" || e.CommandName == "OrderDown")
        {
            if (e.CommandArgument.ToString() != string.Empty)
            {
                //上移下移操作
                string[] strItem = e.CommandArgument.ToString().Split(',');
                result = bisSortInfo.ChangeOrder(
                                    Convert.ToInt32(strItem[0]),
                                    eType,
                                    Convert.ToInt32(strItem[1]),
                                    Convert.ToInt32(strItem[2]),
                                    Convert.ToInt32(strItem[3]));
                if (!result)
                    Until.Alert("操作失败！");
            }

        }
        else if (e.CommandName == "Delete")
        {
            //删除信息
            string[] itemList = e.CommandArgument.ToString().Split(',');
            result = bisSortInfo.Delete(Convert.ToInt32(itemList[0]));
            if (result)
            {
                if (itemList[1] == "0" && eType == ArticelTypeEnum.Disease)
                {
                    new BisBanner().Delete((int)AdverTypeEnum.Banner, Convert.ToInt32(itemList[0]));
                }
                if (itemList.Length > 2 && !string.IsNullOrEmpty(itemList[2]))
                    Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + itemList[2]));
            }
            Until.Alert(result ? "删除成功！" : "删除失败！");
        }

        //重新加载
        if (result)
        {
            InitData();
        }
    }

    /// <summary>
    /// 刷新页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }

    protected string GetCss(object oIndex)
    {
        int nCount = (Convert.ToInt32(oIndex) - 1) * 20;
        if (nCount > 0)
            return "style='padding-left:"+nCount.ToString()+"px'";
        return string.Empty;
    }

    /// <summary>
    /// 获取分类级别
    /// </summary>
    /// <returns></returns>
    protected int GetSortCount()
    {
        switch (eType)
        {
            case ArticelTypeEnum.Disease:
            case ArticelTypeEnum.FeedBack:
            case ArticelTypeEnum.Health:
                return 2;
            case ArticelTypeEnum.Subject:
                return 1;
            default:
                return 0;
        }
    }
}
